<?php
class Dy_model{
	// 添加订阅
	public function dy_add($set_fields)
	{
		$sql = 'INSERT INTO subscribe( ';
		$fields = array(
			'userid' => PDO::PARAM_STR,
			'channelid' => PDO::PARAM_STR,
			'flightid' => PDO::PARAM_STR ,
			'fromairport'=> PDO::PARAM_STR,
			'toairport'=> PDO::PARAM_STR,
			'flydate'=> PDO::PARAM_STR,
			'sub_type'=> PDO::PARAM_STR,
			'updated'=> PDO::PARAM_INT
		);
		$bFirst = TRUE;
		$values = ' VALUES(';
		foreach ($set_fields as $k=> $v){
			if (in_array($k, array_keys($fields)) && isset($v)){
				if (!$bFirst) {
					$sql = $sql.', ';
					$values = $values.', ';
				}
				$bFirst = FALSE;
				$sql = $sql.$k;
				$values = $values.':s_'.$k;
			}
		}
		if ($bFirst){
			return array();
		}
		$sql = $sql.')';
		$values = $values.')';
		$sql = $sql.$values;

		$sth = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		foreach ($set_fields as $k=> $v){
			if (in_array($k, array_keys($fields)) && isset($v)){
				$sth->bindParam(':s_'.$k, $set_fields[$k], $fields[$k]);
				xj_log_message('debug', '['.__METHOD__.'] bindParam :s_'.$k.'='.$v.' PDO::'.strval($fields[$k]));
			}
		}
		xj_log_message('debug', '['.__METHOD__.'] SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
		$ret = $sth->execute();
		if ($ret === FALSE){
			xj_log_message('error', '['.__METHOD__.'] MSG:'.json_encode($sth->errorInfo()).',  SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		if ($sth->rowCount() != 1){
			xj_log_message('error', '['.__METHOD__.'] rowCount:'.json_encode($sth->rowCount()).',  SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		return TRUE;
	}

	// 查询订阅
	public function dy_select($userid = NULL,$offset = 0, $limit = 20, $orderBy = '')
	{
		$sql = 'SELECT * FROM subscribe';
		if ($userid !== NULL)
			$sql = $sql.' WHERE userid=:userid';

		if (! empty($orderBy)){
			$sql .= " ORDER BY ".$orderBy;
		}
		$sql = $sql.' LIMIT :offset, :limit';
		$sth = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		if ($userid !== NULL)
			$sth->bindParam(':userid', $userid, PDO::PARAM_STR);
		$sth->bindParam(':offset', $offset, PDO::PARAM_INT);
		$sth->bindParam(':limit', $limit, PDO::PARAM_INT);
		
		$ret = $sth->execute();
		if ($ret === FALSE){
			xj_log_message('error', '['.__METHOD__.'] MSG:'.json_encode($sth->errorInfo()).', SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return array();
		}
		return  $sth->fetchAll();
	}
	// 删除订阅
	public function dy_del($id)
	{
		$sql = 'DELETE  FROM subscribe';
		$sql = $sql.' WHERE id=:id';

		$sth = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		$sth->bindParam(':id', $id, PDO::PARAM_INT);
		xj_log_message('info', '['.__METHOD__.'] SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
		$ret = $sth->execute();
		if ($ret === FALSE){
			xj_log_message('error', '['.__METHOD__.'] MSG:'.json_encode($sth->errorInfo()).', SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		if ($sth->rowCount() != 1){
			xj_log_message('error', '['.__METHOD__.'] rowCount:'.json_encode($sth->rowCount()).',  SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		return  TRUE;
	}
	// 添加消息
	public function msg_add($set_fields)
	{
		$sql = 'INSERT INTO msg( ';
		$fields = array(
			'userid' => PDO::PARAM_STR,
			'channelid' => PDO::PARAM_STR,
			'flightid' => PDO::PARAM_STR ,
			'fromairport'=> PDO::PARAM_STR,
			'toairport'=> PDO::PARAM_STR,
			'flydate'=> PDO::PARAM_STR,
			//'sub_type'=> PDO::PARAM_STR,
			'msg_id'=> PDO::PARAM_STR,
			'msgtype'=> PDO::PARAM_STR,
			'content'=> PDO::PARAM_STR,
			'updated'=> PDO::PARAM_INT
		);

		$bFirst = TRUE;
		$values = ' VALUES(';
		foreach ($set_fields as $k=> $v){
			if (in_array($k, array_keys($fields)) && isset($v)){
				if (!$bFirst) {
					$sql = $sql.', ';
					$values = $values.', ';
				}
				$bFirst = FALSE;
				$sql = $sql.$k;
				$values = $values.':s_'.$k;
			}
		}
		if ($bFirst){
			return array();
		}
		$sql = $sql.')';
		$values = $values.')';
		$sql = $sql.$values;

		$sth = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		foreach ($set_fields as $k=> $v){
			if (in_array($k, array_keys($fields)) && isset($v)){
				$sth->bindParam(':s_'.$k, $set_fields[$k], $fields[$k]);
				xj_log_message('debug', '['.__METHOD__.'] bindParam :s_'.$k.'='.$v.' PDO::'.strval($fields[$k]));
			}
		}
		xj_log_message('debug', '['.__METHOD__.'] SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
		$ret = $sth->execute();
		if ($ret === FALSE){
			xj_log_message('error', '['.__METHOD__.'] MSG:'.json_encode($sth->errorInfo()).',  SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		if ($sth->rowCount() != 1){
			xj_log_message('error', '['.__METHOD__.'] rowCount:'.json_encode($sth->rowCount()).',  SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		return TRUE;
	}

	// 查询消息
	public function msg_select($userid = NULL,$offset = 0, $limit = 20, $orderBy = '')
	{
		$sql = 'SELECT * FROM msg';
		if ($userid !== NULL)
			$sql = $sql.' WHERE userid=:userid';

		if (! empty($orderBy)){
			$sql .= " ORDER BY ".$orderBy;
		}
		$sql = $sql.' LIMIT :offset, :limit';
		$sth = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		if ($userid !== NULL)
			$sth->bindParam(':userid', $userid, PDO::PARAM_STR);
		$sth->bindParam(':offset', $offset, PDO::PARAM_INT);
		$sth->bindParam(':limit', $limit, PDO::PARAM_INT);

		$ret = $sth->execute();
		if ($ret === FALSE){
			xj_log_message('error', '['.__METHOD__.'] MSG:'.json_encode($sth->errorInfo()).', SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return array();
		}
		return  $sth->fetchAll();
	}
	// 删除消息
	public function msg_del($id)
	{
		$sql = 'DELETE  FROM msg';
		$sql = $sql.' WHERE id=:id';
		$sth = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		$sth->bindParam(':id', $id, PDO::PARAM_INT);
		$ret = $sth->execute();
		if ($ret === FALSE){
			xj_log_message('error', '['.__METHOD__.'] MSG:'.json_encode($sth->errorInfo()).', SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		if ($sth->rowCount() != 1){
			xj_log_message('error', '['.__METHOD__.'] rowCount:'.json_encode($sth->rowCount()).',  SQL:'.$sql.', ARGS:'.json_encode(func_get_args()));
			return FALSE;
		}
		return  TRUE;
	}
}